package com.booby.generic;

/**
 * 功能描述: 接口泛型
 *
 * @author: lizt
 * @date: 2020/8/14 10:42
 **/

public interface GenericInterface<T> {

    T sayHi();
}

/**
 * 功能描述: 泛型接口实现-1
 * 实现类并没有具体类型，使用时需要强制类型
 * @author: lizt
 * @date: 2020/8/16 16:53
**/
class GenericInterfaceImpl<T> implements GenericInterface<T>{

    private T param;

    public GenericInterfaceImpl(T param) {
        this.param = param;
    }

    public GenericInterfaceImpl() {
    }

    public T getParam() {
        return param;
    }

    public void setParam(T param) {
        this.param = param;
    }

    @Override
    public T sayHi() {
        return param;
    }

    public static void main(String[] args) {
        String paramString = "泛型String";
        Integer paramsInt = 111;
        GenericInterface clazz = new GenericInterfaceImpl<Integer>(paramsInt);
        clazz.sayHi();
        Integer param = (Integer)clazz.sayHi();
        System.out.println("Hi:" + param);
    }
}

/**
 * 功能描述: 泛型接口实现-2
 * 实现类已传入具体类型，使用时无需强制类型转化，可直接使用
 *
 * @author: lizt
 * @date: 2020/8/16 16:53
 **/
class GenericInterfaceImpl2 implements GenericInterface<String>{

    private String param;

    public GenericInterfaceImpl2(String param) {
        this.param = param;
    }

    public GenericInterfaceImpl2() {
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    @Override
    public String sayHi() {
        return param;
    }

    public static void main(String[] args) {
        String paramString = "泛型String";
        GenericInterface clazz = new GenericInterfaceImpl2(paramString);
        String param = (String) clazz.sayHi();
//        GenericInterfaceImpl2 clazz = new GenericInterfaceImpl2(paramString);
//        String param = clazz.sayHi();
        System.out.println("Hi:" + param);
    }

}



